package com.zyd.shiro.business.service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;



import com.zyd.shiro.persistence.dao.MallUploadFileDao;
import com.zyd.shiro.persistence.beans.UploadFile;

/**
 * 服务层
 * 
 * @author RookieWZW
 *
 */
@Service
public class MallUploadFileService {

	@Autowired
	private MallUploadFileDao uploadFileDao;
	
	

	public List<UploadFile> findAll() {

		return uploadFileDao.findAll();
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<UploadFile> findPage(int page, int size) {
		PageRequest pageRequest = new PageRequest(page-1, size);
		return uploadFileDao.findAll(pageRequest);
	}

	private Specification<UploadFile> where(Map searchMap) {
		
		return new Specification<UploadFile>() {
          
			@Override
			public Predicate toPredicate(Root<UploadFile> root,CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 
                if (searchMap.get("storage")!=null && !"".equals(searchMap.get("storage"))) {
                	predicateList.add(cb.like(root.get("storage").as(String.class), "%"+(String)searchMap.get("storage")+"%"));
                }
                // 
                if (searchMap.get("file_url")!=null && !"".equals(searchMap.get("file_url"))) {
                	predicateList.add(cb.like(root.get("file_url").as(String.class), "%"+(String)searchMap.get("file_url")+"%"));
                }
                // 
                if (searchMap.get("file_name")!=null && !"".equals(searchMap.get("file_name"))) {
                	predicateList.add(cb.like(root.get("file_name").as(String.class), "%"+(String)searchMap.get("file_name")+"%"));
                }
                // 
                if (searchMap.get("file_type")!=null && !"".equals(searchMap.get("file_type"))) {
                	predicateList.add(cb.like(root.get("file_type").as(String.class), "%"+(String)searchMap.get("file_type")+"%"));
                }
                // 
                if (searchMap.get("extension")!=null && !"".equals(searchMap.get("extension"))) {
                	predicateList.add(cb.like(root.get("extension").as(String.class), "%"+(String)searchMap.get("extension")+"%"));
                }

                return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));
                
            }
        };		
		
	}

	public Page<UploadFile> findSearch(Map whereMap, int page, int size) {
		Specification<UploadFile> specification = where(whereMap);
		PageRequest pageRequest = new PageRequest(page-1, size);
		return uploadFileDao.findAll(specification, pageRequest);
	}

	public UploadFile findOne(Integer id) {
		return uploadFileDao.findOne(id);
	}

	public void add(UploadFile uploadFile) {
		
		uploadFileDao.save(uploadFile);
	}
	
	public void update(UploadFile uploadFile) {
		uploadFileDao.save(uploadFile);
	}

	public void delete(Integer id) {
		uploadFileDao.delete(id);
	}

	public void deleteList(Integer[] ids) {
		for (Integer id : ids) {
			uploadFileDao.delete(id);
		}
	}

}
